/**
 ******************************************************************************
 * @file	adc-t.c
 * @brief	MX287 adc 测试
 *
 *
 * ./adc-t.elf -D /dev/mxs-hsadc0

 *
- 2016-10-09,MenglongWoo,MenglongWoo@aliyun.com

*/

#include <stdio.h> /* using printf() */
#include <stdlib.h> /* using sleep() */
#include <getopt.h>
#include <fcntl.h> /* using file operation */
#include <sys/ioctl.h> /* using ioctl() */
#include <stdbool.h>

#include "proginfo.h"
#include "autoconfig.h"

#define HSADC (0xF)
#define CH0 (0)
#define CH1 (1)
#define CH2 (2)
#define CH3 (3)
#define CH4 (4)
#define CH5 (5)
#define CH6 (6)
#define BATT (7)
#define CORE_TEMP (8)
#define EX_TEMP (9)

const char *hsadc_dev = "/dev/mxs-hsadc0";
const char *lradc_dev = "/dev/mxs-adc";

char *device ;				// 默认设备
bool use_def_dev = true;
int ad_channel = HSADC;			// ADC 通道
int hs_points = 6;			// 高速模式连续采样次数
int times = 10;				// 采样次数
int freq = 1 * 1000 * 1000;		// HSADC通道采样频率，最大 2M

static void print_configure()
{
	printf("device %s\n", device);
	if (ad_channel == HSADC) {
		printf("channel HSADC\n");
	} else {
		printf("channel CH%d\n", ad_channel);
	}

}
static void print_usage(const char *prog)
{
	printf("Usage: %s [-DsbdlHOLC3]\n", prog);
	puts("  -D --device   device to use (default /dev/mxs-hsadc0)\n"
	     "  -c --channel    ADC channel (default HSADC)\n"
	     "\t\tHSADC high speed adc\n"
	     "\t\tCH0-CH6 low speed adc\n"
	    );
	exit(1);
}


static void parse_opts(int argc, char *argv[])
{
	while (1) {
		static const struct option lopts[] = {
			{ "device",  1, 0, 'D' },
			{ "channel", 1, 0, 'c' },
			{ NULL, 0, 0, 0 },
		};
		int c;

		c = getopt_long(argc, argv, "D:c:l:", lopts, NULL);

		if (c == -1) {
			break;
		}
		printf("c %c %c\n", c, optarg[0]);
		switch (c) {
		case 'D':
			device = optarg;
			use_def_dev = false;
			break;
		case 'c':
			switch(optarg[0]) {
			case 'h':		// 高速通道
				ad_channel = HSADC;
				break;
			case '0' ... '9':	// 普通通道
				printf("aasdf\n");
				ad_channel = atoi(optarg);
				break;
			case 'c':		// 核心温度
				break;
			case 'e':		// 外部温度
				break;
			case 'b':		// 电池
				break;
			}

			break;
		default:
			print_usage(argv[0]);
			break;
		}
	}

	// 如果没有指定设备节点，则根据通道选择默认设备节点
	if (true == use_def_dev) {
		if (HSADC == ad_channel ) {
			device = hsadc_dev;
		} else {
			device = lradc_dev;
		}
	}
}

void lradc_test()
{
	int iRes;
	int iTime = 100;
	double val;
	int fd, i;
	fd = open(device, 0);
	if (fd < 0) {
		printf("fail open device %s\n", device);
		return 0;
	}
	short buf[32];


	while(iTime--) {
		sleep(1);
		ioctl(fd, 20, &iRes); /* CH0开启硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH0:%.2f\t", val);

		ioctl(fd, 11, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 1.85) / 4096.0;
		printf("CH1:%.2f\t", val);

		ioctl(fd, 12, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH2:%.2f\t", val);

		ioctl(fd, 13, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH3:%.2f\t", val);

		ioctl(fd, 14, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH4:%.2f\t", val);

		ioctl(fd, 15, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH5:%.2f\t", val);

		ioctl(fd, 26, &iRes); /* CH6开启硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH6:%.2f\t", val);

		ioctl(fd, 17, &iRes); /* 电池电压测量默认开启除4电路*/
		val = (iRes * 7.4) / 4096.0;
		printf("Vbat:%.2f\t", val);
		printf("\n");
	}
	close(fd);
}
void hsadc_test()
{
	short buf[32];
	int fd, i;
	fd = open(device, 0);
	if (fd < 0) {
		printf("fail open device %s\n", device);
		return 0;
	}
	while(times--) {
		read(fd, buf, sizeof(buf));
		// 1.85V 参考电压
		for (i = 0; i < 32; i++) {
			printf("%8.8x\n", buf[i] & 0xfff);// 有效位只有12位，过滤高位的1
		}
		sleep(1);
	}

	close(fd);
}
int main(int argc, char *argv[])
{
	parse_opts(argc, argv);
	print_configure();

	if (ad_channel == HSADC) {
		hsadc_test();
	} else {
		lradc_test();
	}
	int fd;
	int iRes;
	int iTime = 100;
	double val;
	// fd = open("/dev/magic-adc", 0);
	fd = open("/dev/mxs-hsadc0", 0);
	if (fd < 0) {
		printf("open error by APP- %d\n", fd);
		close(fd);
		return 0;
	}
	short buf[32];
	int i;
	while(iTime--) {
		read(fd, buf, sizeof(buf));

		for (i = 0; i < 32; i++) {
			printf("%8.8x\n", buf[i] & 0xfff);
		}
		sleep(1);
	}
	while(iTime--) {
		sleep(1);
		ioctl(fd, 20, &iRes); /* CH0开启硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH0:%.2f\t", val);

		ioctl(fd, 11, &iRes); /* CH1关闭硬件除2电路并读取数据*/
		val = (iRes * 1.85) / 4096.0;
		printf("CH1:%.2f\t", val);

		ioctl(fd, 26, &iRes); /* CH6开启硬件除2电路并读取数据*/
		val = (iRes * 3.7) / 4096.0;
		printf("CH6:%.2f\t", val);

		ioctl(fd, 17, &iRes); /* 电池电压测量默认开启除4电路*/
		val = (iRes * 7.4) / 4096.0;
		printf("Vbat:%.2f\t", val);
		printf("\n");
	}
	close(fd);
}

MODULE_PROJECT(PRJ_NAME);
MODULE_BUILD(__DATE__ " " __TIME__);
PROG_GITSHA1(__GIT_SHA1__);
